{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Game Design\n", "\n", "Today, we will play your Text Adventure game.\n", "\n", "# Generics\n", "\n", "Consider having LinkedLists of different types. We would need:\n", "\n", "```java\n", "public class Node \n", " String data;\n", " Node(String data) {\n", " this.data = data;\n", " }\n", "}\n", "```\n", "\n", "```java\n", "public class Node \n", " Double data;\n", " Node(Double data) {\n", " this.data = data;\n", " }\n", "}\n", "```\n", "\n", "But Java has a better way, called Generics, and it works like this:\n", "\n", "```java\n", "public class Node\n", " T data;\n", " Node(T data) {\n", " this.data = data;\n", " }\n", "}\n", "```\n", "It treats a type as if it were a variable. You fill in the variable when you create an instance:\n", "\n", "```java\n", "Node snode = new Node(\"Hello\");\n", "Node dnode = new Node(34.56);\n", "```\n", "\n", "NOTE: if you use this, you can't assume anything about T. You could though make T implement an interface, though." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Parsing\n", "\n", "\"Parsing\" is just breaking a string up into its parts. We have designed our game files to be easy to parse. We just need to \"split\" a string based on \"::\"." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Added variable line of type String with initial value \"place::pname::This is a description\"\n", "\n" ] } ], "source": [ "String line = \"place::pname::This is a description\";" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Added variable parts of type String[] with initial value [Ljava.lang.String;@5d3411d\n", "\n" ] } ], "source": [ "String[] parts = line.split(\"::\");" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Expression value is: \"place\"\n", "| assigned to temporary variable $4 of type String\n", "\n" ] }, { "data": { "text/plain": [ "place" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "parts[0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Warning**: if any of your names have spaces on either side, you may wish to edit the file to remove them, or to use `String.strip()` to remove them." ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "# Game\n", "\n", "We will need seven classes for our game:\n", "\n", "* [Game class](Game.java) - to start, keep track of where player is\n", "* [Place class](Place.java) - hold places\n", "* [Thing class](Thing.java) - hold the objects in rooms\n", "* [Action class](Action.java) - keep track of the arrows (commands, where they take you)\n", "* [LinkedList](LinkedList.java) - lists of things\n", "* [Stack](Stack.java) - stack of things\n", "* [Node](Node.java) - items in LinkedLists, Stacks" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# In-class Activity\n", "\n", "Download each of the above programs. Open them in the editor by clicking on their names in the Files tab.\n", "\n", "https://athena.brynmawr.edu/jupyter/hub/dblank/public/CS206%20Data%20Structures/2016-Spring/Notebooks\n", "\n", "```shell\n", "%download https://athena.brynmawr.edu/jupyter/hub/dblank/public/CS206%20Data%20Structures/2016-Spring/Notebooks/Game.java\n", "%download https://athena.brynmawr.edu/jupyter/hub/dblank/public/CS206%20Data%20Structures/2016-Spring/Notebooks/Place.java\n", "%download https://athena.brynmawr.edu/jupyter/hub/dblank/public/CS206%20Data%20Structures/2016-Spring/Notebooks/Thing.java\n", "%download https://athena.brynmawr.edu/jupyter/hub/dblank/public/CS206%20Data%20Structures/2016-Spring/Notebooks/Action.java\n", "%download https://athena.brynmawr.edu/jupyter/hub/dblank/public/CS206%20Data%20Structures/2016-Spring/Notebooks/LinkedList.java\n", "%download https://athena.brynmawr.edu/jupyter/hub/dblank/public/CS206%20Data%20Structures/2016-Spring/Notebooks/Stack.java\n", "%download https://athena.brynmawr.edu/jupyter/hub/dblank/public/CS206%20Data%20Structures/2016-Spring/Notebooks/Node.java\n", "```\n", "\n", "In a terminal, you can compile them all with:\n", "\n", "```bash\n", "javac *.java\n", "```\n", "\n", "or one-at-a-time with:\n", "\n", "```bash\n", "javac Game.java\n", "```\n", "\n", "To run the Game:\n", "\n", "```bash\n", "java Game \"filename.game\"\n", "```\n", "\n", "* There are four missing bits of code in Game.java:\n", " * when you read in a new object (ie, Thing), you need to put it in the room\n", " * when you read in a new command (ie, Action), you need to put it in the room\n", " * you need to be able to \"pick up OBJECT\"/\"drop\" an object into/from backpack\n", " * when you attempt to go from one room to another, you need to find that command in the list of actions for that room, make sure you have the required objects/Things, and then move to that room" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Java 9", "language": "java", "name": "java9" }, "language_info": { "file_extension": ".class", "mimetype": "application/java-vm", "name": "java" } }, "nbformat": 4, "nbformat_minor": 0 }